var memberId;//用户ID
var openId;//openId

var pedometeropenId;//微廋openId
window.onload = function() {
	

	setTimeout("timeOutSetScale()",300);
//	deviceBindingState = getParam()['deviceBindingState'];
	openId = getParam()['openId'];
	memberId = getParam()['memberId'];
	var isFirstUse = getParam()['isFirstUse'];
	var countDevicePed = getParam()['countDevicePed'];
	//是否显示多设备按钮
	if(isFirstUse == 0){	
		alertManyWin();
	}else{
		setTimeout("ajaxLoadData('-1')",600);
	}
	document.getElementById("isFirstUse").value = isFirstUse;
	
	if(countDevicePed > 1){
		document.getElementById("manyDeviceBtn").style.display = "block";
	}else{
		document.getElementById("manyDeviceBtn").style.display = "none";
	}

	
	
	
	
	bindSelect();

	bindTouchListener();
//	//底部导航条
//	initNavbar();
	//根据设备绑定selec触发事件
	var name=navigator.platform;
	var downSelect = document.getElementById("downTimeSelect");
	var reSetDownSelect = document.getElementById("reSetDownTimeSelect");
	var resetDateId = document.getElementById("resetDate");
	if(name.substring(0, 6)=="iPhone"||name.substring(0, 4)=="iPod"||name.substring(0, 4)=="iPad"){
//		// iPhone系列
		downSelect.addEventListener("change", function(){return bindUpTimeSelect.apply(this,[1,this.value])}, false);
		reSetDownSelect.addEventListener("change",  function(){return bindResetUpTimeSelect.apply(this,[1,this.value])} , false);
		resetDateId.addEventListener("blur", checkDate, false);
		
		hiddenDOM("resetDateOther");
		showDOM("resetDate");
	}else{
		//Android系统
		downSelect.addEventListener("change", function(){return bindUpTimeSelect.apply(this,[1,this.value])}, false);
		reSetDownSelect.addEventListener("change", function(){return bindResetUpTimeSelect.apply(this,[1,this.value])}, false);
		resetDateId.addEventListener("change",  checkDate , false);
		
	}
//	setTimeout(ajaxLoadData('-1'),600);
};

//关闭页头消息
function closeMessage(){
    var obj = document.getElementById("message");
    if (obj != null){
        obj.style.display = "none";
    }
}



//获取浏览器内核 是否是奇葩- -
function getAndroidUA(){
	var uaName = navigator.userAgent;
	return uaName.substring(uaName.indexOf("; Android") - 1, uaName.indexOf("; Android"));
}
//设置Android下面可选择的最小最大时间
function setResetMaxMinDate(type){
	if(type==0){
		var name=navigator.platform;
		
		if(name.substring(0, 6)=="iPhone"||name.substring(0, 4)=="iPod"||name.substring(0, 4)=="iPad"){
		
		}else{	
			if(getAndroidUA() == "U"){
				//不支持HTML5 input Date的浏览器
				hiddenDOM("resetDate");
				showDOM("resetDateOther");
				bindResetDateOuther();
			}else{
				hiddenDOM("resetDateOther");
				showDOM("resetDate");
				document.getElementById("resetDate").setAttribute("min", getNowDate(sleepDate.firstAnalysisDate.time));
				document.getElementById("resetDate").setAttribute("max", getNowDate(new Date() * 1));
			}
		}
	}
	
	
}
//绑定日期选项框 
function bindResetDateOuther(){
	var dayTime = 24 * 60 * 60 * 1000;
	
	var resetDateOther = document.getElementById("resetDateOther");
	resetDateOther.innerHTML = "";
	var maxTime = new Date( getNowDate( new Date() * 1 ) + " 00:00:00" ) * 1;
	var minTime = new Date(getNowDate(sleepDate.firstAnalysisDate.time) + " 00:00:00") * 1;
	var value = 0;
	var nowTime = new Date( getNowDate(analysisDate.time) + " 00:00:00") * 1; 
	//相差日期数
	var dayCount = (maxTime - minTime) / dayTime; 
//	console.log( getNowDate(maxTime) + "---" + getNowDate(minTime) + "---" + dayCount)
	for (var i = 0; i <= dayCount; i++) {
		if(nowTime == minTime + i * dayTime){
			value = i;
		}
		resetDateOther.innerHTML += '<option value="' + i + '">'+ getNowDate( minTime + i * dayTime) +'</option>';
	}	
//	console.log(value);
//	console.log(getNowDate(analysisDate.time) + "-======")
	document.getElementById("resetDateOther").options[value].selected="true";
}

//延迟设置布局
function timeOutSetScale(){
//	alert("v3...buju");
	setScale();
	
	setScaleTopSleep();
	
	showDOM("mainBodyC");
}

//更改起床时间下拉框index
function changeUpTimeIndex(value){
	lastTWH =   parseInt(value);
}
//绑定起床时间select
function bindUpTimeSelect(type,value) {
	var selectV = 0;
	// var reSetSelectV = 0;
	var upSelect = document.getElementById("upTimeSelect");
	
	var status = -1;	

	if(type == 1){

		var downSelect = document.getElementById("downTimeSelect");
		var downSelectValue = value
		var downSelectText = downSelect.options[downSelectValue].text;

		var upSelect = document.getElementById("upTimeSelect");
		var upSelectVlaue = upSelect.value;
		var upSelectText = upSelect.options[upSelectVlaue].text;

		var downStrs = (downSelectText + "").split(':00');
		var upStrs = (upSelectText + "").split(':00');

		// console.log(downStrs[0] + "---" + Trim( downStrs[1] ) + "---"+ upStrs[0] + "---" + Trim( upStrs[1] ));

		if( Trim(upStrs[1]) == "当天" ){
			//同是一天的时候
			 if( upStrs[0] * 1 < downStrs[0] * 1 ){
			 	// alert("睡眠时长不能超过24小时");
			 	// alert("注意：入睡时间与起床时间相差不能超过24小时");
			 	status = 0;
				// setDefaultSleepTime(0,sleepDate.memberSetTsDate, sleepDate.memberSetTwDate);
				selectV = 7;
				// reSetSelectV = 7;
				// return;
			 }else if(upStrs[0] * 1 == downStrs[0] * 1){
			 	// alert("注意：入睡时间不能跟起床时间相同");
			 	status = 1;
			 	selectV = 7;
			 }else{
			 	 selectV = upStrs[0] * 1 - value - 1;
			 	 selectV = selectV < 0 ? 23 : selectV;

			 	 // reSetSelectV = reSetUpStrs[0] * 1 - value - 1;
			 	 // reSetSelectV = reSetSelectV < 0 ? 23 : reSetSelectV;
			 }

		}else {
			// console.log("---"+ upStrs[0] + "---" + Trim( upStrs[1] ) + "----" +downStrs[0] + "---" + Trim( downStrs[1] ));
			//第二天
			if( upStrs[0] * 1 > downStrs[0] * 1 ){
				// alert("睡眠时长不能超过24小时");
				// alert("注意：入睡时间与起床时间相差不能超过24小时");
				status = 0;
				// setDefaultSleepTime(0,sleepDate.memberSetTsDate, sleepDate.memberSetTwDate);
				selectV = 7;
				// reSetSelectV = 7;
				// return;
			}else{
				selectV = upSelectVlaue - (value - lastTSH) - 1;
				// reSetSelectV = reSetUpSelectVlaue - (value - lastTSH) - 1; 
			}
			
		}
	}


	upSelect.innerHTML = "";
	// reSetSelect.innerHTML = "";
	var timeNumber = 24 - value;
	value++;
	var j = 0;
	for (var i = value; i < 24; i++) {

		if (i < 10) {

			upSelect.innerHTML += '<option value="' + j + '">' + '0' + i
					+ ':00&nbsp;&nbsp;&nbsp;&nbsp;当天</option>';

		} else {

			upSelect.innerHTML += '<option value="' + j + '">' + i
					+ ':00&nbsp;&nbsp;&nbsp;&nbsp;当天</option>';

		}
		j++;
	}

	if (timeNumber <= 24) {

		for (var i = 0; i < value; i++) {

			if (i < 10) {

				upSelect.innerHTML += '<option value="' + j + '">' + '0' + i
						+ ':00&nbsp;&nbsp;&nbsp;&nbsp;次日</option>';

			} else {

				upSelect.innerHTML += '<option value="' + j + '">' + i
						+ ':00&nbsp;&nbsp;&nbsp;&nbsp;次日</option>';

			}
			j++;
		}
	}
	// reSetSelect.innerHTML = upSelect.innerHTML;
	// reSetSelect.value = reSetSelectV;
	// console.log(selectV + "selectV");
	upSelect.value = selectV;
	lastTSH = value;
	if(status == 0){
		alert("注意：入睡时间与起床时间相差不能超过24小时");
	}else if(status ==1){
		alert("注意：入睡时间不能跟起床时间相同");
	}

}

//绑定重设起床时间下拉框
function bindResetUpTimeSelect(type,value){
	var status = -1;
	var reSetSelectV = 0;
	var reSetSelect = document.getElementById("reSetUpTimeSelect");

	if(type == 1){

		var reSetDownSelect = document.getElementById("reSetDownTimeSelect");	
		var reSetDownSelectValue = value;
		var reSetDownSelectText = reSetDownSelect.options[reSetDownSelectValue].text;

		var reSetUpSelect = document.getElementById("reSetUpTimeSelect");
		var reSetUpSelectVlaue = reSetUpSelect.value;
		var reSetUpSelectText = reSetUpSelect.options[reSetUpSelectVlaue].text;

		var reSetDownStrs = (reSetDownSelectText + "").split(':00');
		var reSetUpStrs = (reSetUpSelectText + "").split(':00');

		if( Trim(reSetUpStrs[1]) == "当天" ){
			//同是一天的时候
			 if(reSetUpStrs[0] * 1 < reSetDownStrs[0] * 1){
			 	status = 0;
				reSetSelectV = 7;
			 }else if(reSetUpStrs[0] * 1 == reSetDownStrs[0] * 1){
			 	status = 1;
			 	reSetSelectV = 7;
			 }else{
			 	 reSetSelectV = reSetUpStrs[0] * 1 - value - 1;
			 	 reSetSelectV = reSetSelectV < 0 ? 23 : reSetSelectV;
			 }

		}else {
			// console.log("---"+ upStrs[0] + "---" + Trim( upStrs[1] ) + "----" +downStrs[0] + "---" + Trim( downStrs[1] ));
			//第二天
			if(  reSetUpStrs[0] * 1 > reSetDownStrs[0] * 1){
				status = 0;
				reSetSelectV = 7;
			}else{
				reSetSelectV = reSetUpSelectVlaue - (value - reSetLastTSH) - 1; 
			}
			
		}

	}



	reSetSelect.innerHTML = "";
	var timeNumber = 24 - value;
	value++;
	var j = 0;
	for (var i = value; i < 24; i++) {

		if (i < 10) {

			reSetSelect.innerHTML += '<option value="' + j + '">' + '0' + i
					+ ':00&nbsp;&nbsp;&nbsp;&nbsp;当天</option>';

		} else {

			reSetSelect.innerHTML += '<option value="' + j + '">' + i
					+ ':00&nbsp;&nbsp;&nbsp;&nbsp;当天</option>';

		}
		j++;
	}

	if (timeNumber <= 24) {

		for (var i = 0; i < value; i++) {

			if (i < 10) {

				reSetSelect.innerHTML += '<option value="' + j + '">' + '0' + i
						+ ':00&nbsp;&nbsp;&nbsp;&nbsp;次日</option>';

			} else {

				reSetSelect.innerHTML += '<option value="' + j + '">' + i
						+ ':00&nbsp;&nbsp;&nbsp;&nbsp;次日</option>';

			}
			j++;
		}
	}

	reSetSelect.value = reSetSelectV;
	reSetLastTSH = value;

	if(status == 0){
		alert("注意：入睡时间与起床时间相差不能超过24小时");
	}else if(status ==1){
		alert("注意：入睡时间不能跟起床时间相同");
	}
}

// 设置睡眠质量数据 （上半部）
function setSleepQuality(data, type) {
	var sleepQualityAvgNull = document.getElementById("sleep_quality_avg_null");
	var sleepQualityAvg = document.getElementById("sleep_quality_avg");
    var ringIcon = document.getElementById("ring_zzz_icon");

	var ringDefault = document.getElementById("ring_default");
	var ringDefaultNull = document.getElementById("ring_default_null");
	var ringTimeH = document.getElementById("ring_time_h");
	var ringTimeM = document.getElementById("ring_time_m");
	var sleepLevel = document.getElementById("sleep_level_value");

	var sleepText = document.getElementById("sleepText");

	var sleepAvgDayH = document.getElementById("sleep_avg_h");
	var sleepAvgDayM = document.getElementById("sleep_avg_m");

	var sleepDepthH = document.getElementById("sleep_depth_h");
	var sleepMildH = document.getElementById("sleep_mild_h");
	

	if (data.code != "404") {

		ringDefault.style.color = "#fff";

		if (type == 0) {
			sleepQualityAvgNull.style.display = "none";
			sleepQualityAvg.style.display = "none";
			
			sleepLevel.style.display = "block";
			ringIcon.style.display = "none";
//			ringIcon.style.visibility = "hidden";

			ringDefault.style.display = "block";

			ringDefaultNull.style.display = "none";
			sleepText.style.display = "none";


			ringTimeH.innerHTML = splitHours(data.sleepHoursM / 60);
			ringTimeM.innerHTML = data.sleepHoursM % 60;

			sleepLevel.innerHTML = data.sleepQuality;

			sleepDepthH.innerHTML =  parseFloat( (data.deepSleepHoursM / 60).toFixed(1) );
			

			sleepMildH.innerHTML = parseFloat( (data.shllowSleepHoursM / 60).toFixed(1) );
			

		} else {
			sleepQualityAvgNull.style.display = "none";
			sleepQualityAvg.style.display = "block";
			
			sleepLevel.style.display = "none";
			ringIcon.style.display = "block";
//			ringIcon.style.visibility = "visible";

			sleepText.style.display = "block";
			ringDefault.style.display = "block";
			ringDefaultNull.style.display = "none";

			ringTimeH.innerHTML = splitHours(data.weekSleep / 60);
			ringTimeM.innerHTML = data.weekSleep % 60;

			sleepAvgDayH.innerHTML = splitHours(data.avgWeekDay / 60)
			sleepAvgDayM.innerHTML = data.avgWeekDay % 60;
			sleepLevel.innerHTML = data.avgWeekDay;

			sleepDepthH.innerHTML = parseFloat( (data.deepSleepHoursM / 60).toFixed(1) );
			

			sleepMildH.innerHTML = parseFloat( (data.shllowSleepHoursM / 60).toFixed(1) );
			

		}
	} else {

		ringDefaultNull.style.display = "block";
		ringDefault.style.display = "none";
		// ringDefault.innerHTML = "暂无数据";
		sleepDepthH.innerHTML = "0";
		// sleepDepthM.innerHTML = "0";
		sleepMildH.innerHTML = "0";
		// sleepMildM.innerHTML = "0";
		if (type == 0) {

			sleepQualityAvgNull.style.display = "none";
			sleepQualityAvg.style.display = "none";
			// sleepQuality.style.display = "block";
			sleepText.style.display = "none";
			sleepLevel.style.display = "block";
			 ringIcon.style.display = "none";
//			ringIcon.style.visibility = "hidden";
			sleepLevel.innerHTML = "";
			

		} else {
			sleepText.style.display = "block";
			sleepLevel.style.display = "none";
			ringIcon.style.display = "block";
//			ringIcon.style.visibility = "visible";
			// sleepQuality.style.display = "none";
			sleepQualityAvg.style.display = "none";
			sleepQualityAvgNull.style.display = "block";
			
		}
		
	}

}
//切换天周的时候动态设置文字位置
function setTopFooterCoo(type){
//	console.log(type + "type---------");
	var topH = document.getElementById("mainTopC").offsetHeight;
	var topHH = document.getElementById("mainTopHeaerC").offsetHeight;
	var topCH = document.getElementById("mainTopCenterC").offsetHeight;
	var topF = document.getElementById("mainTopFooterC");
	var deepMildBig = document.getElementById("deepMildBig");
	var topFH =  topH - topHH - topCH;
	var dmH = deepMildBig.offsetHeight;
	topF.style.height = topFH + "px";
	if(type == 0){
		deepMildBig.style.marginTop = topFH / 2 - dmH / 2 + "px"; 
	}else{
		deepMildBig.style.marginTop = 0 + "px"; 
	}
	
	// console.log(topH - topHH - topCH + "px")
}


var analysisDate;// 当前时间
// 天周切换
function changeDayOrWeek() {

	// console.log("天周切换");
	var dayOrWeek = document.getElementById("dayOrWeek");
//	var onDayOrOnWeek = document.getElementById("onDayOrOnWeek");
	
	if (dayOrWeek.innerHTML == "天") {
		var time = new Date() * 1;
		var dateTime = getNowTimeC(time);
		ajaxLoadData('1', dateTime);
//		dayOrWeek.innerHTML = "周";
//		removeTouchListener();
//		
//		hiddenDOM("mainTopLeftBtnC");
//		document.getElementById("resetTimeBtn").style.visibility = "hidden";
	} else {
		var time = new Date() * 1;
		var dateTime = getNowTimeC(time);
		ajaxLoadData('0', dateTime);
//		dayOrWeek.innerHTML = "天";	
//
//		//控制左上角图标显示
//		showDOM("mainTopLeftBtnC");
//		document.getElementById("mainTopLeftBtnC").style.margin = "auto";
//		setScaleTopHeaderImg();
//		var setTime = document.getElementById("mainLeftBtnC");
//		setTime.addEventListener("touchstart", alertSleepTime, false);
//
//		document.getElementById("resetTimeBtn").style.visibility = "visible";
//		var ringTitle = document.getElementById("ringTitle");
//		ringTitle.addEventListener("touchstart", alertResetWin, false);
		
	}
	changeDayOrWeekShow();
}

//切换天周的显示效果
function changeDayOrWeekShow(){
	var dayOrWeek = document.getElementById("dayOrWeek");
	if(dayOrWeek.innerHTML == "天"){
		
		dayOrWeek.innerHTML = "周";
		removeTouchListener();
		hiddenDOM("mainTopLeftBtnC");
		document.getElementById("resetTimeBtn").style.visibility = "hidden";
		
	}else{
		
		dayOrWeek.innerHTML = "天";	
		//控制左上角图标显示
		showDOM("mainTopLeftBtnC");
		
		document.getElementById("mainTopLeftBtnC").style.margin = "auto";
		setScaleTopHeaderImg();
		var setTime = document.getElementById("mainLeftBtnC");
		setTime.addEventListener("touchstart", alertSleepTime, false);

		document.getElementById("resetTimeBtn").style.visibility = "visible";
		var ringTitle = document.getElementById("ringTitle");
		ringTitle.addEventListener("touchstart", alertResetWin, false);
	}
}

// 设置睡眠时间
function setSleepTime() {
	removeInputFocus();
	// console.log("设置睡眠时间");
	var dayOrWeekType = document.getElementById("dayOrWeek").innerHTML;

	var downSelect = document.getElementById("downTimeSelect");
	var downSelectValue = downSelect.value;
	var downSelectText = downSelect.options[downSelectValue].text;

	var upSelect = document.getElementById("upTimeSelect");
	var upSelectVlaue = upSelect.value;
	var upSelectText = upSelect.options[upSelectVlaue].text;

	var downStrs = (downSelectText + "").split(':00');
	var upStrs = (upSelectText + "").split(':00');

	// console.log( upStrs[0] + "---" + Trim(upStrs[1]) + "====" + downStrs[0] +
	// "---" + Trim(downStrs[1]));

	var nowDate = getNowDate(new Date() * 1);

	downTime = nowDate + " " + downStrs[0] + ":00:00";

	if (Trim(upStrs[1]) == "当天") {
		upTime = nowDate + " " + upStrs[0] + ":00:00";
	} else {
		upTime = getNowDate(new Date() * 1 + 24 * 60 * 60 * 1000) + " "
				+ upStrs[0] + ":00:00";
	}

	if (dayOrWeekType == "天") {
		dayOrWeekType = "day";
	} else {
		dayOrWeekType = "week";
	}

	// console.log("downTime:" + downTime + "----upTime:" + upTime
	// +"---nowDateTime:" + getNowTimeC(analysisDate.time) + "dayOrWeekType:" +
	// dayOrWeekType);

	ajaxLoadData(2, getNowTimeC(analysisDate.time), downTime, upTime,
			dayOrWeekType);

	removeBg();

}
var lastTSH;//上一次入睡时间
var lastTWH;//上一次起床时间
var tsDateH;//当前入睡时间
var twDateH;//睡觉起床时间
//设置默认睡眠时间
function setDefaultSleepTime(type,tsDate,twDate) {
	// var tsDateH;
	
	var upSelect = document.getElementById("upTimeSelect");

	// var reSetUpSelect = document.getElementById("reSetUpTimeSelect");
	
	tsDate && (tsDateH = tsDate.hours);
	twDate && (twDateH = twDate.hours);

	tsDateH = tsDateH != null ? tsDateH : 22;
	twDateH = twDateH != null ? twDateH : 6;

	lastTSH = tsDateH;
	lastTWH = twDateH;

	// console.log( "type:" + type + "----tsDateH:" + tsDateH + "---twDateH:" + twDateH +"-----=====");

	bindUpTimeSelect(type,tsDateH,twDateH);

	if(twDateH <= tsDateH){
		upSelect.value =  twDateH + (24 - tsDateH) - 1;
	}else{
		upSelect.value =  twDateH - tsDateH - 1;
	}
	// console.log( "tsDateH:" + tsDateH + "---twDateH:" + twDateH);
	document.getElementById('downTimeSelect').value = tsDateH;


}
var reSetTSDateH;//重设入睡时间
var reSetTWDateH;//重设起床时间
var reSetLastTSH;//重设上次入睡时间
var reSetLastTWH;//重设上次起床时间
function setReSetSleepTime(type,resetTsDate,resetTwDate){
	var reSetUpSelect = document.getElementById("reSetUpTimeSelect");



	resetTsDate && (reSetTSDateH = resetTsDate.hours);
	resetTwDate && (reSetTWDateH = resetTwDate.hours);
	// console.log(reSetTSDateH + "----" + reSetTWDateH);
	reSetTSDateH = reSetTSDateH != -1 ? reSetTSDateH : tsDateH;
	reSetTWDateH = reSetTWDateH != -1 ? reSetTWDateH : twDateH; 

	reSetLastTSH = reSetTSDateH;
	reSetLastTWH = reSetTWDateH;
	// console.log(reSetTSDateH + "----" + reSetTWDateH + "----ts" + tsDateH + "---tw" + twDateH);
	bindResetUpTimeSelect(type,reSetTSDateH,reSetTWDateH);

	if( reSetTWDateH <= reSetTSDateH ){

		reSetUpSelect.value = reSetTWDateH + (24 - reSetTSDateH) - 1;
	}else{

		reSetUpSelect.value =  reSetTWDateH - reSetTSDateH - 1;
	}

	// console.log(type+"---"+reSetTSDateH + "----" + reSetTWDateH)
	
	document.getElementById("reSetDownTimeSelect").value = reSetTSDateH;

}

//绑定入睡时间下拉框数据
function bindSelect() {
	var downSelect = document.getElementById("downTimeSelect");
	var reSetSelect = document.getElementById("reSetDownTimeSelect");

	for (var i = 0; i < 24; i++) {
		if (i < 10) {
			downSelect.innerHTML += '<option value="'
					+ i
					+ '">'
					+ '0'
					+ i
					+ ':00&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;当天</option>';
		} else {
			downSelect.innerHTML += '<option value="'
					+ i
					+ '">'
					+ i
					+ ':00&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;当天</option>';
		}

	}
	reSetSelect.innerHTML = downSelect.innerHTML;
};

//刷新页面数据 （上一页 下一页）
function refreshPageDate(num) {
	var dayOrWeek = document.getElementById("dayOrWeek").innerHTML;
	if (dayOrWeek == "天") {

		var time = analysisDate.time * 1 + 24 * 60 * 60 * 1000 * num;
		var dateTime = getNowTimeC(time);
		ajaxLoadData('0', dateTime);
	} else {
		var time = analysisDate.time * 1 + 24 * 60 * 60 * 1000 * 7 * num;
		var dateTime = getNowTimeC(time);
		ajaxLoadData('1', dateTime);
	}
}

//重设睡眠时间刷新数据
function resetSleepData(){
	removeInputFocus();
	window.clearInterval(resetTimeID);
	var downSelect = document.getElementById("reSetDownTimeSelect");
	var downSelectValue = downSelect.value;
	var downSelectText = downSelect.options[downSelectValue].text;

	var upSelect = document.getElementById("reSetUpTimeSelect");
	var upSelectVlaue = upSelect.value;
	var upSelectText = upSelect.options[upSelectVlaue].text;

	var downStrs = (downSelectText + "").split(':00');
	var upStrs = (upSelectText + "").split(':00');

		
		
	var resetDate = document.getElementById("resetDate").value;
	
	if(getAndroidUA() == "U"){
		//不支持HTML5 input Date的浏览器
		var otherSelect = document.getElementById("resetDateOther");
		var otherSelectValue = otherSelect.value;
		resetDate = otherSelect.options[otherSelectValue].text;
		console.log(resetDate +"日期 --------");
	}
	// console.log(resetDate+"------"+typeof(resetDate));
	downTime = resetDate + " " + downStrs[0] + ":00:00";

	if (Trim(upStrs[1]) == "当天") {
		upTime = resetDate + " " + upStrs[0] + ":00:00";
	} else {
		upTime = getNowDate(new Date(resetDate) * 1 + 24 * 60 * 60 * 1000) + " "
				+ upStrs[0] + ":00:00";
	}

	// console.log("downTime:" + downTime + "----upTime:" + upTime
	// +"---nowDateTime:" + getNowTimeC(analysisDate.time));

	reSetTSDateH = downStrs[0] * 1;
	reSetTWDateH = upStrs[0] * 1;
	// console.log("downStrs:" + reSetTSDateH + "---upStrs:" + reSetTWDateH)
	// setReSetSleepTime(0);
	setReSetSleepTime(0,sleepDate.daySetTsDate,sleepDate.daySetTwDate);

	removeBg();

	ajaxLoadData(3, getNowTimeC(new Date(resetDate) * 1), downTime, upTime);
	
	

}

function checkDate(){
	var value = document.getElementById("resetDate").value + " 00:00:00";
//	console.log(getNowDate(sleepDate.firstAnalysisDate.time));
	if(value){
		if(new Date(value) * 1 > new Date() * 1){
			alert("设置时间不能大于当前的时间！");
			document.getElementById("resetDate").value = getNowDate(new Date() * 1);
		}else if(new Date(value) * 1 < new Date(getNowDate(sleepDate.firstAnalysisDate.time) + " 00:00:00")){
			alert("设置时间不能小于最早的时间！");
			document.getElementById("resetDate").value = getNowDate(new Date() * 1);
		}	
	}else{
		document.getElementById("resetDate").value = getNowDate(new Date() * 1);	
	}
}

var sleepDate;//睡眠数据
//初始化加载数据
function ajaxLoadData(type, time, beginTime, endTime, dayOrWeekType,changeDevice) {
	var deviceId;
	document.getElementById("deviceId") && (deviceId = document.getElementById("deviceId").value);
	deviceId = deviceId ? deviceId : "";
	alertLoaderWin();
	var xhr = createXmlHttpRequest();
	xhr.onreadystatechange = function() {

		if (xhr.readyState == 4) {
			if (xhr.status == 200) {
				var json = xhr.responseText;
				var jsonData = eval("(" + json + ")");//转换后的JSON对象
				sleepDate = jsonData;

				console.log(sleepDate);
				// console.log(new Date("2014-10-01") * 1)
				var dayOrWeek = document.getElementById("dayOrWeek").innerHTML;
				if (dayOrWeek == "天") {
					type = 0;
				} else {
					type = 1;
				}
//				//如果切换设备  数据强制切换成当天的数据
							
				setSleepQuality(sleepDate, type);

				analysisDate = sleepDate.analysisDate;

				setDefaultSleepTime(0,sleepDate.memberSetTsDate,
						sleepDate.memberSetTwDate);
				
				setResetMaxMinDate(type);
				
				reSetTSDateH = -1;
				reSetTWDateH = -1;
				setReSetSleepTime(0,sleepDate.daySetTsDate,sleepDate.daySetTwDate);
				
				showTitleText(type, analysisDate.time);
				
				drawTreeC(canvasW, canvasH, sleepDate,bgColor,type);
				
				setTopFooterCoo(type);;
				removeLoader();
			}
		}
	};
	var url = "/api/ajax/getSleepDataOfPage";
	switch (parseInt(type)) {
	case 0:
	case 1:
		//天 周 上下一页切换
		xhr.open("post", url + "?pedometeropenId=" + openId + "&type=" + type
				+ "&time=" + time +"&deviceId="+ deviceId+"&clear=" + new Date() * 1, true);
		break;
	case 2:
		//设置睡眠时间
		xhr.open("post", url + "?pedometeropenId=" + openId + "&type=" + type
				+ "&time=" + time + "&beginTime=" + beginTime + "&endTime="
				+ endTime + "&dayOrWeekType=" + dayOrWeekType +"&deviceId="+ deviceId + "&clear="
				+ new Date() * 1, true);
		break;
	case 3:
		xhr.open("post", url + "?pedometeropenId=" + openId + "&type=" + type
				+ "&time=" + time + "&beginTime=" + beginTime + "&endTime="
				+ endTime +"&deviceId="+ deviceId+ "&clear="
				+ new Date() * 1, true);
		break;
	case 4:
		xhr.open("post", url + "?pedometeropenId=" + openId + "&type=" + type
				+"&deviceId="+ deviceId+ "&changeDevice=" + changeDevice + "&clear=" + new Date() * 1, true);
		break;
	default:
		var isFirstUse = document.getElementById("isFirstUse").value;
		//初始化
		xhr.open("post", url + "?pedometeropenId=" + openId + "&type=" + type
				+"&deviceId="+ deviceId+"&isFirstUse="+ isFirstUse +"&clear=" + new Date() * 1, true);
		break;
	}

	xhr.send();
}

//标题显示效果
function showTitleText(type, time) {
	var date;
	showDOM("mainNextImgDC");
	if (type == 0) {
		//计算 今天 昨天
		var  nowDateTime = getNowDate(new Date() * 1 );
		var dateTime = getNowDate(time);
//		console.log("nowDate:" + new Date(nowDateTime) + "---dateTime:" + new Date(dateTime));
		date = (new Date(nowDateTime) * 1 - new Date(dateTime) * 1) / (24 * 60 * 60 * 1000);
		if (date < 1) {
			document.getElementById("onDayOrOnWeek").innerHTML = "今天";
			// document.getElementById("mainNextImgDC").style.display = "none";
			hiddenDOM("mainNextImgDC");
		} else if (date < 2) {
			document.getElementById("onDayOrOnWeek").innerHTML = "昨天";
		} else {
			document.getElementById("onDayOrOnWeek").innerHTML = getNowDate(time);
		}
	} else {
		//计算本周  上周
		var nowDate = new Date();
		var nowWeekStart = new Date(getWeekStartDate(nowDate * 1)) * 1;//本周开始时间
		var nowWeekEnd = new Date(getWeekEndDate(nowDate * 1)) * 1;//本周结束时间

		var lastDate = nowDate * 1 - (24 * 60 * 60 * 1000 * 7);
		var lastWeekStart = new Date(getWeekStartDate(lastDate)) * 1;//上周开始时间
		var lastWeekEnd = new Date(getWeekEndDate(lastDate)) * 1;//上周结束时间	

		date = new Date( getNowDate(time) ) * 1;
		var weekStart = new Date(getWeekStartDate(date)) * 1;
		var weekEnd = new Date(getWeekEndDate(date)) * 1;
		// console.log( getNowDate( nowWeekEnd )+ "---" +getNowDate( date )+ "---" + getNowDate( nowWeekStart));
		if (date <= nowWeekEnd && date >= nowWeekStart) {
			//本周
			document.getElementById("onDayOrOnWeek").innerHTML = "本周";
			// document.getElementById("mainNextImgDC").style.display = "none";
			hiddenDOM("mainNextImgDC");
		} else if (date <= lastWeekEnd && date >= lastWeekStart) {
			//上周				
			document.getElementById("onDayOrOnWeek").innerHTML = "上周";
		} else {
			document.getElementById("onDayOrOnWeek").innerHTML = getNowMonthDay(weekStart)
					+ "~" + getNowMonthDay(weekEnd);
		}
		// console.log(date + "---"+ weekStart + "---" + weekEnd);
	}

	//控制上一页按钮显示/隐藏
	if(sleepDate.dataPrevious){
		showDOM("mainPreImgDC");

	}else{
		hiddenDOM("mainPreImgDC");
	}

}
//弹出加载图标层
function alertLoaderWin(){
	var alertWin = document.getElementById("ajaxLoader");
	alertWin.style.display = "block";

	var mybg = document.createElement("div");
	mybg.setAttribute("id", "alert_bg");
	mybg.style.background = "#000";
	mybg.style.width = "100%";
	mybg.style.height = document.body.scrollHeight + "px";
	mybg.style.position = "fixed";
	mybg.style.overflow = "hidden";
	mybg.style.top = "0";
	mybg.style.left = "0";
	mybg.style.zIndex = "2500";
	mybg.style.opacity = "0.1";

	document.body.appendChild(mybg);
	document.body.style.overflow = "hidden";
	removeTouch(mybg.id);
}

//控制重设时间为空时候赋值
var resetTimeID;
//刷新重设时间
function refreshResetDate(){
//	console.log("更新时间线程启动");
	var resetDate = document.getElementById("resetDate");
	var value = resetDate.value;
	if(value.length <= 0 ){
		resetDate.value = getNowDate(analysisDate.time);
	}
}


//初始化重设睡眠时间选择框数据
function bindResetDateSelect(_date){
	var resetDateSelect = document.getElementById("resetDateSelect");
	resetDateSelect.innerHTML = "";
//	console.log(_date);
	var nowDateTime = analysisDate.time;
	var nowDay = getNowDate(new Date() * 1);

	var dayCount = (new Date() * 1 - _date) / (24 * 60 * 60 * 1000);
//	console.log(_date + "----" + dayCount + "---" + (_date - nowDateTime));
	if(dayCount > 10){
		dayCount = 10;
	}
	var dateTime;
	for(var i = 1; i <= dayCount; i++){
		dateTime = getNowDate( new Date() * 1 - 24 * 60 * 60 * 1000 * (dayCount - i) );
		resetDateSelect.innerHTML += '<option value="' + dateTime + '">' + dateTime + '</option>';
	}
//	console.log(getNowDate(_date))
	resetDateSelect.value = getNowDate(_date);
}

//移除所有input的焦点
function removeInputFocus(){
	// document.getElementById("reSetDownTimeSelect").blur();
	// document.getElementById("reSetUpTimeSelect").blur();
	// document.getElementById("downTimeSelect").blur();
	// document.getElementById("upTimeSelect").blur();
	// document.getElementById("resetDate").blur();
//	document.getElementById("reSetDownTimeSelect").focus();
//	document.getElementById("downTimeSelect").focus();
	isFocus("reSetDownTimeSelect");
	isFocus("reSetUpTimeSelect");
	isFocus("downTimeSelect");
	isFocus("upTimeSelect");
	isFocus("resetDate");
}

function isFocus(_id){
	if(document.activeElement.id == _id){
		document.getElementById(_id).blur();
	}
}


//去掉字符串前后空格
function Trim(str)
{ 
    return str.replace(/(^\s*)|(\s*$)/g, ""); 
	}

//隐藏弹出框 and bg
function removeBg(){
	var mybg = document.getElementById("alert_bg");
	var alertWin = document.getElementById("alertSleepWin");
	var alertResetWin = document.getElementById("alertResetWin");
	// var ajaxLoader = document.getElementById("ajaxLoader");
	var alertSleepTimeWin = document.getElementById("alertSleepTimeWin");
	mybg && (document.body.removeChild(mybg));
	alertWin && (hiddenDOM("alertSleepWin"));
	alertResetWin && (hiddenDOM("alertResetWin"));
	alertSleepTimeWin && (hiddenDOM("alertSleepTimeWin"));
}
/**
 * 隐藏弹出框
 * @param alertWinId
 */
function removeBg2(alertWinId){
	var mybg = document.getElementById("alert_bg");
	var alertWin = document.getElementById(alertWinId);
	mybg && (document.body.removeChild(mybg));
	alertWin && (hiddenDOM(alertWinId));
}

//隐藏加载图层
function removeLoader(){
	var mybg = document.getElementById("alert_bg");
	var ajaxLoader = document.getElementById("ajaxLoader");
	mybg && (document.body.removeChild(mybg));
	ajaxLoader && (hiddenDOM("ajaxLoader"));
}

//睡眠时间弹出框
function alertSleepTime(){
	alertWin('alertSleepWin');
	// alertWin("alertSleepTimeWin");

	// changeSettingTime(0);
}



//弹出框
function alertWin(id){
	var alertWin = document.getElementById(id);
	alertWin.style.display = "block";

	var mybg = document.createElement("div");
	mybg.setAttribute("id", "alert_bg");
	mybg.style.background = "#000";
	mybg.style.width = "100%";
	mybg.style.height = document.body.scrollHeight + "px";
	mybg.style.position = "fixed";
	mybg.style.overflow = "hidden";
	mybg.style.top = "0";
	mybg.style.left = "0";
	mybg.style.zIndex = "2500";
	mybg.style.opacity = "0.3";

	document.body.appendChild(mybg);
	document.body.style.overflow = "hidden";
	removeTouch(mybg.id);
}

//上一页
function previous(){	
	refreshPageDate(-1);
}
//下一页
function nextPage(){	
	refreshPageDate(1);
}
//重设睡眠时间框
function alertResetWin(){
	
	if(getAndroidUA() == "U"){
		
		//document.getElementById("resetDateOther").options[valueOther].selected="true";
	}else{
		document.getElementById("resetDate").value = getNowDate(analysisDate.time);
		 
		resetTimeID = setInterval("refreshResetDate()",1000);	
	}
	
	// showDateInput();
	alertWin('alertResetWin');
}
//常规睡眠时间取消
function closeSleepWin(){
	removeInputFocus();
	removeBg();
	setDefaultSleepTime(0,sleepDate.memberSetTsDate,
				sleepDate.memberSetTwDate);
	setReSetSleepTime(0);
}
function closeResetWin(){
	
	window.clearInterval(resetTimeID);
	
	removeInputFocus();
	removeBg();
	setDefaultSleepTime(0,sleepDate.memberSetTsDate,
				sleepDate.memberSetTwDate);	
	setReSetSleepTime(0);
}

var settingCount = -1;//防止重复触发该事件
//切换常规、重设 睡眠时间
function changeSettingTime(type){
	// document.getElementById("resetSleepTimeWin");

	var alertSleepTimeWin =  document.getElementById("alertSleepTimeWin");
	var settingDefTime = document.getElementById("settingDefTime");
	var settingResetTime = document.getElementById("settingResetTime");

	if(type == 0 && settingCount != 0){
		
		hiddenDOM("resetSleepTimeWin");
		alertSleepTimeWin.style.height = "220px";
		// alertSleepTimeWin.style.marginTop = "-110px";
		settingDefTime.style.color = "#f7872c";
		settingResetTime.style.color = "#848484";
		showDOM("sleepTimeWin");
		settingCount = 0;
	}else if(type == 1 && settingCount != 1){

		hiddenDOM("sleepTimeWin");
		alertSleepTimeWin.style.height = "270px";
		// alertSleepTimeWin.style.marginTop = "-145px";
		settingDefTime.style.color = "#848484";
		settingResetTime.style.color = "#f7872c";
		showDOM("resetSleepTimeWin");
		settingCount = 1;
	}
	
}
//绑定touch事件
function bindTouchListener(){

	var setTime = document.getElementById("mainLeftBtnC");
	setTime.addEventListener("touchstart", alertSleepTime, false);

	var changeTime = document.getElementById("mainRightBtnC");
	changeTime.addEventListener("touchstart", changeDayOrWeek, false);

	var pre = document.getElementById("mainPreImgDC");
	pre.addEventListener("touchstart", previous, false);

	var next = document.getElementById("mainNextImgDC");
	next.addEventListener("touchstart", nextPage, false);

	var cancel = document.getElementById("alertWinCancelBtn");
	cancel.addEventListener("touchstart", closeSleepWin, false);

	var ok = document.getElementById("alertWinOkBtn");
	ok.addEventListener("touchstart", setSleepTime, false);

	// var reset = document.getElementById("mainCenterRingC");
	// reset.addEventListener("touchstart", alertResetWin, false);

	var resetCancel = document.getElementById("alertWinCancelBtn_reSet");
	resetCancel.addEventListener("touchstart", closeResetWin, false);

	var resetOk = document.getElementById("alertWinOkBtn_reSet");
	resetOk.addEventListener("touchstart", resetSleepData, false);

	// var settingDefTime = document.getElementById("settingDefTime");
	// settingDefTime.addEventListener("touchstart", function(){return changeSettingTime.apply(this,[0])} , false);

	// var settingResetTime = document.getElementById("settingResetTime");
	// settingResetTime.addEventListener("touchstart", function(){return changeSettingTime.apply(this,[1])} , false);

	var ringTitle = document.getElementById("ringTitle");
	ringTitle.addEventListener("touchstart", alertResetWin, false);

}
//移除触摸事件
function removeTouchListener(){
	var reset = document.getElementById("mainCenterRingC");
	reset.removeEventListener("touchstart",alertResetWin, false);			

	var setTime = document.getElementById("mainLeftBtnC");
	setTime.removeEventListener("touchstart", alertSleepTime, false);			

	var ringTitle = document.getElementById("ringTitle");
	ringTitle.removeEventListener("touchstart", alertResetWin, false);
}

//获取当前时间字符串 yyyy-MM-dd HH:mm:ss
function getNowTimeC(time){
	var date = new Date(time);
	var year = date.getFullYear();
	var month = date.getMonth() + 1;
	var day = date.getDate();
	var hour = date.getHours(); 
	var minute = date.getMinutes(); 
	var second = date.getSeconds(); 
	var timeStr=year
		+ "-" + (month<10 ? "0" + month : month)
		+ "-" + (day<10 ? "0"+ day : day)
		+ " " + (hour<10 ? "0"+ hour : hour)
		+ ":" + (minute<10 ? "0" + minute : minute)
		+ ":" + (second<10 ? "0" +second : second);
	return timeStr;
}
//格式化时间 HH:mm
function formatHourM(h,m){
	var timeStr= (h<10 ? "0"+ h : h)
		+ ":" + (m<10 ? "0" + m : m)
	return timeStr;
}
//返回年月日 yyyy-MM-dd
function getNowDate(time){
	var date = new Date(time);
	var year = date.getFullYear();
	var month = date.getMonth() + 1;
	var day = date.getDate();
	var timeStr=year
		+ "-" + (month<10 ? "0" + month : month)
		+ "-" + (day<10 ? "0"+ day : day)
	return timeStr;
}
//返回月日 MM月dd日
function getNowMonthDay(time){
	var date = new Date(time);
	var month = date.getMonth() + 1;
	var day = date.getDate();
	var timeStr= (month<10 ? "0" + month : month)
		+ "月" + (day<10 ? "0"+ day : day) + "日"
	return timeStr;
}

//获得本周的开端日期 yyyy-MM-dd
function getWeekStartDate(time) { 
	var date = new Date(time);
	var nowYear = date.getFullYear();
	var nowMonth = date.getMonth();
	var nowDay = date.getDate();
	var nowDayOfWeek = date.getDay() == 0 ? 7 : date.getDay();
	var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek + 1); 
	return formatDate(weekStartDate); 
}

//获得本周的停止日期 yyyy-MM-dd
function getWeekEndDate(time) { 
	var date = new Date(time);
	var nowYear = date.getFullYear();
	var nowMonth = date.getMonth();
	var nowDay = date.getDate();
	var nowDayOfWeek = date.getDay() == 0 ? 7 : date.getDay();
	var weekEndDate = new Date(nowYear, nowMonth, nowDay + (7 - nowDayOfWeek)); 
	return formatDate(weekEndDate); 
}

//格局化日期：yyyy-MM-dd 
function formatDate(date) { 
	var myyear = date.getFullYear(); 
	var mymonth = date.getMonth() + 1; 
	var myweekday = date.getDate();

	if(mymonth < 10){ 
		mymonth = "0" + mymonth; 
	} 
	if(myweekday < 10){ 
		myweekday = "0" + myweekday; 
	} 
	return (myyear+"-"+mymonth + "-" + myweekday); 
}
//截取小数点之前的数 m = 2.3 return 2
function splitHours(m){
	return (m + "").split('.')[0];
};
//弹出多设备提示层
function alertManyWin(){
//	var alertWin = document.getElementById(id);
//	alertWin.style.display = "block";
	//背景
	var mybg = document.createElement("div");
	mybg.setAttribute("id", "alert_bg_tips");
	mybg.style.background = "black";
	mybg.style.width = "100%";
	mybg.style.height = document.body.scrollHeight + "px";
	mybg.style.position = "fixed";
	mybg.style.overflow = "hidden";
	mybg.style.top = "0";
	mybg.style.left = "0";
	mybg.style.zIndex = "2500";
	mybg.style.opacity = "0.7";
	
	//提示操作文字块
	var manyTipsText = document.createElement("div");
	manyTipsText.id = "manyTipsId";
	manyTipsText.style.width = "50%";
	manyTipsText.style.position = "fixed";
	manyTipsText.style.left = "8%";
	manyTipsText.style.top = "23%";
	manyTipsText.style.lineHeight = "30px";
	manyTipsText.style.fontSize = "16px";
	manyTipsText.style.color = "#fff";
	manyTipsText.style.zIndex = "2555";
	//文字内容
	var tipsText = document.createElement("p");
	tipsText.innerHTML = "点击这里，选择您要查看的手环睡眠数据。";
	
	//箭头图片
	var arrow = document.createElement("img");
	arrow.id = "arrowId";
	arrow.src = "../../img/device/rr.png";
	arrow.style.width = "85px";
	arrow.style.position = "fixed";
	arrow.style.top = "18%";
	arrow.style.right = "12%";
	arrow.style.zIndex = "2555";
	
	//确认图标
	var bottomDiv = document.createElement("div");
	bottomDiv.id = "tipsOkBtn"; 
	bottomDiv.style.width = "100%";
	bottomDiv.style.height = "35px";
	bottomDiv.style.position = "fixed";
	bottomDiv.style.bottom = "65px";
//	bottomDiv.style.background = "blue";
	bottomDiv.style.textAlign = "center";
	bottomDiv.style.zIndex = "2555";
	
	
	var okBtn = document.createElement("div");
	okBtn.style.margin = "auto";
	okBtn.style.width = "90px";
	okBtn.style.height = "32px";
	okBtn.style.lineHeight = "32px";
	okBtn.style.color = "#fff";
	okBtn.style.fontSize = "18px";
	okBtn.style.background = "#8dd25f";
	okBtn.style.borderRadius = "32px";	
	okBtn.innerHTML = "知道了";
	okBtn.addEventListener("touchstart", closeTipsBg, false);
	bottomDiv.appendChild(okBtn);
	
	//把文字添加到块里面
	manyTipsText.appendChild(tipsText);
	
	document.body.appendChild(manyTipsText);
	document.body.appendChild(arrow);
	document.body.appendChild(bottomDiv);
	
	
	document.getElementById("manyDeviceBtnTipsDOM").style.display = "block";
//	showDOM("manyDeviceBtnTipsDOM");
	
	document.body.appendChild(mybg);
	document.body.style.overflow = "hidden";
	removeTouch(mybg.id);
}
//关闭背景层
function closeTipsBg(){
	
	var tipsBg = document.getElementById("alert_bg_tips");
	var arrowId = document.getElementById("arrowId");
	var manyTipsId = document.getElementById("manyTipsId");
	var tipsOkBtn = document.getElementById("tipsOkBtn");
	var manyTipsDOM =  document.getElementById("manyDeviceBtnTipsDOM");
	arrowId && (document.body.removeChild(arrowId));
	manyTipsId && (document.body.removeChild(manyTipsId));
	tipsOkBtn && (document.body.removeChild(tipsOkBtn)); 
	tipsBg && (document.body.removeChild(tipsBg));
	manyTipsDOM && (hiddenDOM("manyDeviceBtnTipsDOM"));
	
	ajaxLoadData('-1');
}
//切换设备弹出框
function alertManyDeviceWin(){
	var alertWin = document.getElementById("alert_manyDevice_win");
	alertWin.style.display = "block";
	
	var mybg = document.createElement("div");
	mybg.setAttribute("id", "alert_bg");
	mybg.style.background = "#000";
	mybg.style.width = "100%";
	mybg.style.height = document.body.scrollHeight + "px";
	mybg.style.position = "fixed";
	mybg.style.overflow = "hidden";
	mybg.style.top = "0";
	mybg.style.left = "0";
	mybg.style.zIndex = "2500";
	mybg.style.opacity = "0.1";

	document.body.appendChild(mybg);
	document.body.style.overflow = "hidden";	
	removeTouch(mybg.id);
	removeTouch("alert_manyDevice_win");
	bindChangeDeviceTouchmove();
}
//alertManyWin();
var selectedId;//选中的设备ID
//多设备选中效果
function manyDeviceSelected(_selectedId){
	
//	console.log(selectedId);
	selectedId = _selectedId;
	document.getElementById(_selectedId).style.background = "#e1e1e1";
	var deviceList = getElementsByClassName("alertManyDeviceItem");
//	console.log(deviceList);
	for(var i = 0; i < deviceList.length; i++){
		
		if(deviceList[i].id != _selectedId){
			deviceList[i].style.background = "#fff";
		}
	}
}

/**
 * 根据className获取所有对象
 * @param className
 * @returns {Array}
 */
function getElementsByClassName(className) {
	var all = document.all ? document.all : document.getElementsByTagName('*');
	var elements = new Array();
	for (var e = 0; e < all.length; e++) {
		if (all[e].className == className) {
			elements[elements.length] = all[e];
		}
	}
	return elements;
}

//获取多设备列表数据
function ajaxGetManyDevices(){
	var xhr = createXmlHttpRequest();
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4) {
			if (xhr.status == 200) {
				var json = xhr.responseText;
				var jsonData = eval("(" + json + ")");//转换后的JSON对象
				
//				console.log(jsonData.length);
				setManyDeviceItem(jsonData);
				alertManyDeviceWin();
			}
		}
	};
	var url = ctx + "/wechat/ajax_sleep_devices_servlet";
	xhr.open("post", url + "?openId=" + openId + "&memberId="+memberId+"&clear=" + new Date() * 1, true);
	xhr.send();
}

//显示多设备列表
function showManyDeviceWin(){
	ajaxGetManyDevices();
}

//填充列表数据
function setManyDeviceItem(json){
	var data = json.data;
	var mdl = document.getElementById("alertManyDeviceList");
	var html = "";
	for(var i = 0; i < data.length; i++){
		if(json.dafultDeviceId.toLocaleLowerCase() == data[i].id.toLocaleLowerCase()){
			html += '<div id="'+data[i].id+'" class="alertManyDeviceItem" style="background:#e1e1e1;" onclick="manyDeviceSelected(this.id)">';
		}else{
			html += '<div id="'+data[i].id+'" class="alertManyDeviceItem" onclick="manyDeviceSelected(this.id)">';
		}
			html +='<div class="amdItemLeft">';
			var modeNum = data[i].modelNum;
			var deviceImg = "bonbon.png";
			if(modeNum.toLowerCase().indexOf("405", 0)>=0){
				deviceImg = "manbo.png";
				html +='<img src="'+ctx+'/v3/img/device/'+ deviceImg +'" alt="" width="60" style="margin-top: 5px;"/>';
			}else if(modeNum.toLowerCase().indexOf("407", 0)>=0){
				deviceImg = "bonbon.png";
				html +='<img src="'+ctx+'/v3/img/device/'+ deviceImg +'" alt="" width="60" style="margin-top: 14px;"/>';
			}else if(modeNum.toLowerCase().indexOf("408", 0)>=0){
				deviceImg = "kband.png";
				html +='<img src="'+ctx+'/v3/img/device/'+ deviceImg +'" alt="" width="60" style="margin-top: 8px;"/>';
			}else if(modeNum.toLowerCase().indexOf("410", 0)>=0){
				deviceImg = "bonbon_c.png";
				html +='<img src="'+ctx+'/v3/img/device/'+ deviceImg +'" alt="" width="60" style="margin-top: 4px;"/>';
			}		
			html += '</div>'
			+'<div class="amdItemCenter">'
			+'<p style="font-size: 16px;margin-top: 15px;" class="textHidden">'+data[i].deviceName+'</p>'
			+'<p style="font-size: 12px;">SN:'+data[i].sn+'</p>'
			+'</div>'
			+'<div class="amdItemRight">'
			+'<img src="'+ctx+'/v3/img/setting_arrow.png" alt="" width="40" style="margin-top: 13px;" />'
			+'</div>'
			+'</div>';
	}
	mdl.innerHTML = html;
}

//切换设备数据
function changeManyDevice(){
	selectedId && (document.getElementById("deviceId").value = selectedId);
//	changeDayOrWeekShow();
	
	var dayOrWeek = document.getElementById("dayOrWeek").innerHTML;
	var type = 0;
	if (dayOrWeek == "天") {
		type = 0;
	} else {
		type = 1;
	}
	ajaxLoadData('4',null,null,null,null,type);
	
	
	removeBg2("alert_manyDevice_win");
}
/**
 * iphone禁止滑动：
 * @param id 浮层
 */
function removeTouch(id){
	document.getElementById(id).addEventListener("touchmove", tmove, false);
	function tmove(ev){
		ev.preventDefault();
	}
}

/**
 * 解除iPhone禁止滑动：
 * @param id 浮层
 */
function bindTouch(id){
	document.getElementById(id).addEventListener("touchmove", function(){}, false);
}
/**
 * 绑定手环列表的touch事件
 */
function bindChangeDeviceTouchmove(){
	//alertManyDeviceList
	document.getElementById("alertManyDeviceList").addEventListener("touchstart", manyDeviceTouchstart, false);
	document.getElementById("alertManyDeviceList").addEventListener("touchmove", manyDeviceTouchmove, false);
	//document.getElementById("alertManyDeviceList").addEventListener("touchend", manyDeviceTouchend, false);
}
var mdScrollTop;//手环列表滚动高度
var mdStartY;//开始位置
/**
 * 添加切换手环的touchstart事件
 */
function manyDeviceTouchstart(ev){
	//ev.preventDefault();
	mdStartY = ev.touches[0].pageY;
}
/**
 * 添加切换手环的touchmove事件
 */
function manyDeviceTouchmove(ev){
	ev.preventDefault();
	//滑动位置
	var mdMoveY = ev.touches[0].pageY;
	if(mdStartY - mdMoveY > 0){
		document.getElementById("alertManyDeviceList").scrollTop += 6;
	}else{
		document.getElementById("alertManyDeviceList").scrollTop -= 6;
	}
}
/**
 * 添加切换手环的touchend事件
 */
function manyDeviceTouchend(ev){
	
}
